package com.example.bookstore.service.Impl;

import com.example.bookstore.common.api.CommonResult;
import com.example.bookstore.mbg.mapper.BookMapper;
import com.example.bookstore.mbg.mapper.DealMapper;
import com.example.bookstore.mbg.mapper.ManagerMapper;
import com.example.bookstore.mbg.model.*;
import com.example.bookstore.service.ManagerService;
import com.example.bookstore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: rain
 * @date: 2020/5/9 19:30
 * @description:
 */
@Service
public class ManagerServiceImpl implements ManagerService {


    @Autowired
    private ManagerService managerService;

    @Autowired
    private UserService userService;

    @Autowired(required = false)
    private ManagerMapper managerMapper;
    @Autowired(required = false)
    private DealMapper dealMapper;

    @Autowired(required = false)
    private BookMapper bookMapper;

    @Override
    public CommonResult login(String managerid, String password) {
        Manager manager = managerMapper.selectByPrimaryKey(managerid);
        System.out.println(manager.getMpassword());
        if (!manager.getMpassword().equals(password)){
            System.out.println("管理员密码错误");
            return CommonResult.failed("管理员密码错误");
        }
        else {
            System.out.println("管理员登录成功");
            return CommonResult.success(managerid, "管理员登录成功");
        }
    }

    @Override
    public CommonResult deleteCommentByDealId(String Dealid) {
        Deal deal = dealMapper.selectByPrimaryKey(Dealid);
        String text="已被删除";
        deal.setComment(text);

        dealMapper.updateByPrimaryKeySelective(deal);
        return CommonResult.success(Dealid,"评论删除成功");
    }

    @Override
    public CommonResult ModifyUserInformation(User user) {
        return userService.ModifyUserInformation(user);
    }

    @Override
    public CommonResult AddBook(String bookid,String bname,String Descri,String author,String Press,double price,String url,int num) {
        if(bname.length()==0)
        {
            return CommonResult.failed("书本名不能为空");
        }
        Book book = new Book();
        book.setBookid(bookid);
        book.setAuthor(author);
        book.setBname(bname);
        book.setDescri(Descri);
        book.setPrice(price);
        book.setPress(Press);
        book.setNum(num);
        book.setUrl(url);

        bookMapper.insert(book);
        return CommonResult.success(bname,"添加成功");
    }

    @Override
    public CommonResult DeleteBookByName(String bname) {
        BookExample bookExample = new BookExample();
        bookExample.createCriteria().andBnameEqualTo(bname);

        bookMapper.deleteByExample(bookExample);

        return CommonResult.success(bname,"删除成功");
    }

    @Override
    public CommonResult ModifyBook(String bookid,String bname,String Descri,String author,String Press,double price,String url,int num) {
        Book b= bookMapper.selectByPrimaryKey(bookid);

        b.setBname(bname);
        b.setUrl(url);
        b.setNum(num);
        b.setPrice(price);
        b.setPress(Press);
        b.setDescri(Descri);
        b.setAuthor(author);

        bookMapper.updateByPrimaryKeySelective(b);

        return CommonResult.success(bname,"修改成功");
    }

    @Override
    public CommonResult AddNumInBook(String bookid,String bookname,int num) {
         Book book = bookMapper.selectByPrimaryKey(bookid);

         book.setNum(num);

         bookMapper.updateByPrimaryKeySelective(book);

        return CommonResult.success(bookid,"增加成功");
    }
}
